package com.jamal2362.apktool.preference;

import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import androidx.appcompat.app.AlertDialog;
import com.jamal2362.apktool.R;

public class RadioGroupPreference extends CustomPreference
{
	private int dialogTitle;
	private String defaultValue;
	private String[] entries;
	private String[] entryValues;

	public RadioGroupPreference(Context ctx, AttributeSet a)
	{
		super(ctx, a);
	}
	
	private boolean initEntries()
	{
		final Context context = getContext();
		final String key = getKey();
		if (key.equals("date_format"))
		{
			dialogTitle = R.string.date_format;
			defaultValue = "yyyy/MM/dd HH:mm";
			entries = context.getResources().getStringArray(R.array.date_variants);
			entryValues = context.getResources().getStringArray(R.array.date_variants);
			return entries.length == entryValues.length;
		}
		else if (key.equals("lang"))
		{
			dialogTitle = R.string.LangDialogTitle;
			defaultValue = "default";
			entries = context.getResources().getStringArray(R.array.entries_lang);
			entryValues = context.getResources().getStringArray(R.array.values_lang);
			return entries.length == entryValues.length;
		}
		else if (key.equals("auto_save"))
		{
			dialogTitle = R.string.auto_save_title;
			defaultValue = "15";
			entries = context.getResources().getStringArray(R.array.auto_save_variants);
			entryValues = context.getResources().getStringArray(R.array.auto_save_values);
			return entries.length == entryValues.length;
		}
		else if (key.equals("editor_theme"))
		{
			dialogTitle = R.string.editor_cs;
			defaultValue = "default";
			entries = context.getResources().getStringArray(R.array.editor_themes);
			entryValues = context.getResources().getStringArray(R.array.editor_themes_values);
			return entries.length == entryValues.length;
		}
		return false;
	}

	@Override
	protected void onClick()
	{
		if (!initEntries())
		{
			return;
		}
		final Context context = getContext();
		final String key = getKey();
		final String def = getSharedPreferences().getString(key, defaultValue);
		int select = -1;
		for (int i = 0; i < entryValues.length; i++)
		{
			if (entryValues[i].equals(def))
			{
				select = i;
				break;
			}
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(dialogTitle);
		builder.setPositiveButton(R.string.cancel, null);
		builder.setNeutralButton(R.string.search_reset, new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					getSharedPreferences().edit().putString(key, defaultValue).commit();
					dialog.dismiss();
				}
			});
		builder.setSingleChoiceItems(entries, select, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item)
                {
                    getSharedPreferences().edit().putString(key, entryValues[item]).commit();
					dialog.dismiss();
                }
            }
		);
		builder.create().show();
	}
}
